package cn.iocoder.yudao.module.wms.controller.admin.stockdetail;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.stockdetail.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.stockdetail.StockDetailDO;
import cn.iocoder.yudao.module.wms.service.stockdetail.StockDetailService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 库存明细")
@RestController
@RequestMapping("/wms/stock-detail")
@Validated
public class StockDetailController {

    @Resource
    private StockDetailService stockDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建库存明细")
    @PreAuthorize("@ss.hasPermission('wms:stock-detail:create')")
    public CommonResult<Long> createStockDetail(@Valid @RequestBody StockDetailSaveReqVO createReqVO) {
        return success(stockDetailService.createStockDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存明细")
    @PreAuthorize("@ss.hasPermission('wms:stock-detail:update')")
    public CommonResult<Boolean> updateStockDetail(@Valid @RequestBody StockDetailSaveReqVO updateReqVO) {
        stockDetailService.updateStockDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:stock-detail:delete')")
    public CommonResult<Boolean> deleteStockDetail(@RequestParam("id") Long id) {
        stockDetailService.deleteStockDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:stock-detail:query')")
    public CommonResult<StockDetailRespVO> getStockDetail(@RequestParam("id") Long id) {
        StockDetailDO stockDetail = stockDetailService.getStockDetail(id);
        return success(BeanUtils.toBean(stockDetail, StockDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存明细分页")
    @PreAuthorize("@ss.hasPermission('wms:stock-detail:query')")
    public CommonResult<PageResult<StockDetailRespVO>> getStockDetailPage(@Valid StockDetailPageReqVO pageReqVO) {
        PageResult<StockDetailDO> pageResult = stockDetailService.getStockDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存明细 Excel")
    @PreAuthorize("@ss.hasPermission('wms:stock-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportStockDetailExcel(@Valid StockDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockDetailDO> list = stockDetailService.getStockDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存明细.xls", "数据", StockDetailRespVO.class,
                        BeanUtils.toBean(list, StockDetailRespVO.class));
    }

}